﻿
CREATE PROCEDURE sp_InsertFilteredEvent
	-- Add the parameters for the stored procedure here
	@MeterNumber nvarchar,
	@MeterVendorID nvarchar,
	@MeterHeadEndID nvarchar,
	@TimeStamp datetime,
	@EventType nvarchar,
	@Reading float,
	@Temp float,
	@Code nvarchar,
	@Status nvarchar
	--@Exists int OUTPUT
AS
BEGIN
	SELECT
		[MeterNumber]
	   ,[MeterVendorID]
	   ,[MeterHeadEndID]
	FROM
		[AMIDataStore].[dbo].[FilteredEvents]
	WHERE
		[MeterNumber]=@MeterNumber and
		[MeterVendorID] = @MeterVendorID and
		[MeterHeadEndID] = @MeterHeadEndID and
		[EventType] =@EventType and
		[Status] = @Status

	IF @@ROWCOUNT > 0
		BEGIN
			--SELECT @Exists=1
			RETURN 1
		END
	ELSE
		BEGIN
			INSERT INTO [AMIDataStore].[dbo].[FilteredEvents]
				   (
				    [MeterNumber]
				   ,[MeterVendorID]
				   ,[MeterHeadEndID]
				   ,[TimeStamp]
				   ,[EventType]
				   ,[Reading]
				   ,[Temp]
				   ,[Code]
				   ,[Status]
				   )
			 VALUES
				   (
				   @MeterNumber,
				   @MeterVendorID,
				   @MeterHeadEndID,
				   @TimeStamp,
				   @EventType,
				   @Reading,
				   @Temp,
				   @Code,
				   @Status
				   )
			--SELECT @Exists=0
			RETURN 0
		END
END
